﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.Form
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;
    using SMP.Domain;

    public partial class FrmMathang : SMP.Windows.Forms.FrmBase
    {
        Mathang_Service _objMathang = new Mathang_Service();
        Mathang mathang = new Mathang();
        DataTable dtb_mathang = new DataTable();
        DataTable dtb_nhom_mathang = new DataTable();
        DataTable dtb_donvi_tinh = new DataTable();
        public bool Is_Add { set; get; }
        public bool Is_Xuatkho { set; get; }
        public DataRow[] SelectedRow { set; get; }

        public FrmMathang()
        {
            InitializeComponent();
            item_Print.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            Is_Add = false;
            Is_Xuatkho = false;
        }

        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {
                this.FormState = Windows.Forms.FormState.View;
                if (Is_Add)
                {
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                    item_Add.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    item_Edit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    item_Delete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    item_Save.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    item_Cancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    tableLayoutPanel2.Visible = true;
                    colChon.Visible = true;
                }
                else
                {
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    tableLayoutPanel2.Visible = false;
                    colChon.Visible = false;
                }

                if (!Is_Xuatkho)
                    dtb_nhom_mathang = _objMathang.GetAll_Nhom_Mathang();
                else
                    dtb_nhom_mathang = _objMathang.GetAll_Mathang_Xuatkho();

                lookNhom_Mathang.Properties.DataSource = dtb_nhom_mathang;
                gridLook_Nhom_Mathang.DataSource = dtb_nhom_mathang;

                dtb_donvi_tinh = _objMathang.GetAll_Donvi_Tinh();
                lookDonvi_Tinh.Properties.DataSource = dtb_donvi_tinh;
                gridLook_Donvi_Tinh.DataSource = dtb_donvi_tinh;

                dtb_mathang = _objMathang.GetAll_Mathang();
                if (!dtb_mathang.Columns.Contains("Chon"))
                    dtb_mathang.Columns.Add("Chon", typeof(bool));
                dgMathang.DataSource = dtb_mathang;

                ClearDataBindings();
                ResetText();
                ChangeStatus(true);
                if (gvMathang.RowCount > 0)
                    DataBindingControl();

            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ResetText()
        {
            lookNhom_Mathang.EditValue = null;
            txtTen_Mathang.EditValue = null;
            lookDonvi_Tinh.EditValue = null;
            txtThongtin_Mathang.EditValue = null;

            lookNhom_Mathang.Focus();
        }

        public override void ChangeStatus(bool editTable = true)
        {
            lookNhom_Mathang.Properties.ReadOnly = editTable;
            txtTen_Mathang.Properties.ReadOnly = editTable;
            lookDonvi_Tinh.Properties.ReadOnly = editTable;
            txtThongtin_Mathang.Properties.ReadOnly = editTable;
        }

        public override void ClearDataBindings()
        {
            lookNhom_Mathang.DataBindings.Clear();
            txtTen_Mathang.DataBindings.Clear();
            lookDonvi_Tinh.DataBindings.Clear();
            txtThongtin_Mathang.DataBindings.Clear();
        }

        public override void DataBindingControl()
        {
            lookNhom_Mathang.DataBindings.Add("EditValue", dtb_mathang, ".Id_Nhom_Mathang");
            txtTen_Mathang.DataBindings.Add("EditValue", dtb_mathang, ".Ten_Mathang");
            lookDonvi_Tinh.DataBindings.Add("EditValue", dtb_mathang, ".Id_Donvi_Tinh");
            txtThongtin_Mathang.DataBindings.Add("EditValue", dtb_mathang, ".Thongtin_Mathang");
        }

        #endregion


        #region PerformAdd, PerformEdit, PerformDelete, PerformCancel, PerformSave, PerformSelectOneObject

        public override bool PerformAdd()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            return true;
        }

        public override bool PerformEdit()
        {
            ChangeStatus(false);
            return true;
        }

        public override bool PerformDelete()
        {
            var del = false;
            if (_objMathang.Get_Mathang_Check_Used_By_Id_Mathang(gvMathang.GetFocusedRowCellValue("Id_Mathang")))            
                Windows.Forms.Message.Show("Mặt hàng đã sử dụng, không thể xóa", "Thông Báo");
            
            if(Windows.Forms.Message.Show("Bạn có thực sự muốn xóa "+txtTen_Mathang.EditValue+" ", "Thông Báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                del = _objMathang.Delete_Mathang(gvMathang.GetFocusedRowCellValue("Id_Mathang"));
            if (del)
                DisplayInfo();
            return true;
        }

        public override bool PerformCancel()
        {
            ClearDataBindings();
            DisplayInfo();
            return true;
        }

        public override bool PerformSave()
        {
            var save = false;
            Hashtable hashtableControl = new Hashtable();
            hashtableControl.Add(txtTen_Mathang, lbTen_Mathang);
            hashtableControl.Add(lookNhom_Mathang, lbNhom_Mathang);
            hashtableControl.Add(lookDonvi_Tinh, lbDonvi_Tinh);
            if (!Windows.Utility.Validator.CheckNullFields(hashtableControl))
                return false;
            if (dtb_mathang != null && dtb_mathang.Rows.Count > 0)
            {
                DataRow[] row = dtb_mathang.Select(string.Format("Ten_Mathang = '{0}'", txtTen_Mathang.EditValue));
                if (row.Length > 0)
                {
                    Windows.Forms.Message.Show("Đơn vị tính " + txtTen_Mathang.EditValue + " đã tồn tại.\nVui lòng xem lại", "Thông Báo", MessageBoxButtons.OK);
                    return false;
                }
            }

            mathang.Id_Nhom_Mathang = lookNhom_Mathang.EditValue;
            mathang.Id_Donvi_Tinh = lookDonvi_Tinh.EditValue;
            mathang.Ten_Mathang = txtTen_Mathang.EditValue;
            mathang.Thongtin_Mathang = txtThongtin_Mathang.EditValue;

            if (this.FormState == Windows.Forms.FormState.Add)
                save = _objMathang.Insert_Mathang(mathang);
            else
                save = _objMathang.Update_Mathang(mathang);
            if (save)
                DisplayInfo();

            return true;
        }
                
        public override object PerformSelectOneObject()
        {
            SelectedRow = dtb_mathang.Select("Chon = 'True'");

            if (SelectedRow == null)
            {
                Windows.Forms.Message.Show("Chưa chọn Mặt hàng.\nVui lòng chọn Mặt hàng", "Thông Báo", MessageBoxButtons.OK);
                return null;
            }
            this.Close();
            this.Dispose();
            return true;
        }
        
        #endregion


        #region Method

        private void lookNhom_Mathang_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
            {
                MasterForm.FrmNhom_Mathang _frmNhom_Mathang = new MasterForm.FrmNhom_Mathang();
                _frmNhom_Mathang.StartPosition = FormStartPosition.CenterScreen;
                _frmNhom_Mathang.ShowDialog();

                if (_frmNhom_Mathang.SelectedRow != null)
                {
                    if (dtb_nhom_mathang.Select(string.Format("Id_Nhom_Mathang = {0}", _frmNhom_Mathang.SelectedRow["Id_Nhom_Mathang"])).Length == 0)
                    {
                        dtb_nhom_mathang =_objMathang.GetAll_Nhom_Mathang();
                        lookNhom_Mathang.Properties.DataSource = dtb_nhom_mathang;
                    }                    
                    lookNhom_Mathang.EditValue = _frmNhom_Mathang.SelectedRow["Id_Nhom_Mathang"];                    
                }
            }
        }

        private void lookDonvi_Tinh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
            {
                MasterForm.FrmDonvi_Tinh _frmDonvi_Tinh = new MasterForm.FrmDonvi_Tinh();
                _frmDonvi_Tinh.StartPosition = FormStartPosition.CenterScreen;
                _frmDonvi_Tinh.ShowDialog();

                if (_frmDonvi_Tinh.SelectedRow != null)
                {
                    if (dtb_donvi_tinh.Select(string.Format("Id_Donvi_Tinh = {0}", _frmDonvi_Tinh.SelectedRow["Id_Donvi_Tinh"])).Length == 0)
                    {
                        dtb_donvi_tinh = _objMathang.GetAll_Donvi_Tinh();
                        lookDonvi_Tinh.Properties.DataSource = dtb_donvi_tinh;
                    }
                    lookDonvi_Tinh.EditValue = _frmDonvi_Tinh.SelectedRow["Id_Donvi_Tinh"];
                }
            }
        }

        #endregion

        private void gvMathang_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.FieldName == "STT")
                    e.DisplayText = (Convert.ToInt64(e.RowHandle) + 1).ToString();
            }
        }

        private void ckCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataRow r in dtb_mathang.Rows)
            {
                r["Chon"] = ckCheckAll.EditValue;
            }
            if (ckCheckAll.EditValue + "" == "true")
                gvMathang.OptionsBehavior.AutoExpandAllGroups = false;
            else
                gvMathang.OptionsBehavior.AutoExpandAllGroups = true;
        }

        private void gridChon_CheckedChanged(object sender, EventArgs e)
        {
            DataRow[] row = dtb_mathang.Select(string.Format("Id_Mathang = {0}", gvMathang.GetFocusedRowCellValue("Id_Mathang")));
            row[0]["Chon"] = gridChon.Editable;
        }

        



        



    }
}